package org.ogv.collision;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Pool;

public class BoundingSphereGroup extends BoundingSphere {
	
	/////////// Object management //////////////////////////////////////////////////////
	private static Pool<BoundingSphereGroup> _POOL = new Pool<BoundingSphereGroup>() {
		@Override
		protected BoundingSphereGroup newObject() {
			return new BoundingSphereGroup();
		}
	};
	public static void clearPool() 				{ _POOL.clear(); }
	
	public static BoundingSphereGroup obtain() 	{ 
		return _POOL.obtain();
	}
	
	public void free()							{ 
		this.clearSpheres();
		_POOL.free(this); 
	}
	
	protected BoundingSphereGroup() {
		_spheres = new ArrayList<BoundingSphere>();
	}
	
	
	
	
	
	public float rotation;
	private ArrayList<BoundingSphere> _spheres;
	
	public void addSphere(BoundingSphere s) {
		_spheres.add(s);
	}
	
	public void clearSpheres() {
		for(BoundingSphere s : _spheres) s.free();
		_spheres.clear();
	}
	
	@Override
	public void debugDraw(SpriteBatch sb, float offsetX, float offsetY) {		
		for(BoundingSphere s : _spheres) s.debugDraw(sb, offsetX - this.x, offsetY - this.y);
	}
	
	@Override
	public boolean isTouch(BoundingSphere other) {
		other.x -= this.x;
		other.y -= this.y;
		boolean ret = false;
		
		for(BoundingSphere s : _spheres) {
			if(s.isTouch(other)) {
				ret = true;
				break;
			}
		}

		other.x += this.x;
		other.y += this.y;
		return ret;
	}
	
	@Override
	public boolean isTouch(float x, float y) {
		for(BoundingSphere s : _spheres) if(s.isTouch(x - this.x, y - this.y)) return true;
		return false;
	}
}
